package ev.agent;
import ev.space.*;
import ev.traffic.*;
import swarm.defobj.Zone;
import java.util.*;
import swarm.collections.List;
import swarm.gui.ZoomRaster;
import swarm.objectbase.SwarmObjectImpl;
public class WorkCar extends SwarmObjectImpl implements Car{
		private Substation workPlace,homePlace,sub;
		private float soc;
		private CitySpace citySpace;
		private int carNum;
		private Nodes node;
		private Side curSide;
		private java.util.List<Integer> roadLine;
		private Owner owner;
		private CarProxy carProxy;
		private byte color;
		private int state,preState;
		private double cost = 0;
		private java.util.List<Double> costs = new java.util.ArrayList<Double>();
		public WorkCar(Zone aZone,float soc,int carNum,CitySpace citySpace,Substation homePlace){
				super(aZone);
				this.soc = soc;
				this.state = Car.STOP;
				this.carNum = carNum;
				this.citySpace = citySpace;
				this.homePlace = homePlace;
				this.sub = homePlace;
				this.sub.addCar(this);
				roadLine = new ArrayList<Integer>();
				owner = new WorkOwner(this);
				carProxy = new CarProxy(this);
Loop:			while(true){
						Random ran = new Random();
						int length = SubSpace.subList.getCount();
						int rand = ran.nextInt(length);
						workPlace = (Substation)SubSpace.subList.atOffset(rand);
						if(homePlace.equals(workPlace)){
								continue Loop;
						}
						break;
				}
		}
		public Substation getHome(){
				return this.homePlace;
		}
		public Substation getWork(){
				return this.workPlace;
		}
		public void setSub(Substation sub){
				this.sub = sub;
		}
		public void leaveSub(){
				this.sub.removeCar(this);
				this.setState(Car.STOP);
		}
		public Substation getSub(){
				return this.sub;
		}
		public void setState(int state){
				int preState = this.getState();
				this.setPreState(preState);
				this.state = state;
		}
		public int getState(){
				return this.state;
		}
		public void setPreState(int preState){
				this.preState = preState;
		}
		public int getPreState(){
				return this.preState;
		}
		public void setNode(Nodes node){
				this.node = node;
		}
		public Nodes getNode(){
				return this.node;
		}
		public void setSoc(float soc){
				this.soc = soc;
		}
		public float getSoc(){
				return this.soc;
		}
		public void setRoadLine(java.util.List<Integer> roadLine){
				this.roadLine = roadLine;
		}
		public java.util.List<Integer> getRoadLine(){
				return this.roadLine;
		}
		public void setCurSide(Side side){
				this.curSide = side;
		}
		public Side getCurSide(){
				return this.curSide;
		}
		public CitySpace getCitySpace(){
				return this.citySpace;
		}
		public Object setCarColor(byte color){
				this.color = color;
				return this;
		}
		public Object drawSelfOn(ZoomRaster r){
				int xPos = this.node.getXpos();
				int yPos = this.node.getYpos();
				r.drawPointX$Y$Color(xPos,yPos,this.color);
				return this;
		}
		public double getAverageCost(){
			double result = 0;
			for(double d:costs){
					result += d;
			}
			if(costs.size() == 0){
					return 0;
			}
			result /= costs.size();
			return result;
	}
		public void chooseSub() {
				boolean flag = owner.ifChoose();
				if(!flag){
						return;
				}
				this.owner.chooseSub();
				this.setState(Car.RUNNING);
				int x = this.sub.getXpos();
				int y = this.sub.getYpos();
				int destNum = ((Nodes)citySpace.getObjectAtX$Y(x, y)).getNum();
				int startPoint = node.getNum();
				roadLine = LineDecision.getRoadLine(startPoint, destNum);
				cost = this.getSoc();
		}
		public void drive(){
				if(state == Car.RUNNING ){
						boolean flag = carProxy.drive();
						if(flag == false){
								this.setState(Car.STOP);
								if(this.getSub() != null){
										double temp = this.getSoc();
										cost = cost - temp;
										costs.add(cost);
										this.sub.addCar(this);
								}
						}
				}
			}
		public void charge(){
				carProxy.charge();
				owner.changeState();
		}
		public void randomDrive(){
			
		}
		@Override
		public Owner getOwner() {
			// TODO Auto-generated method stub
			return this.owner;
		}
}
